package com.kwt.learn.multithread;

/**
 * 三个线程顺序执行输出 使用 synchronize 和 wait
 */
public class Solution03 {

    private static Object lock = new Object();

    private static volatile boolean threadA = false;
    private static volatile boolean threadB = false;

    public static void main(String[] args) {

        Thread a = new Thread(() -> {
            synchronized (lock) {
                System.out.println("a");
                threadA = true;
                lock.notifyAll();
            }
        });

        Thread b = new Thread(() -> {

            synchronized (lock) {
                if (!threadA) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("b");
                threadB = true;
                lock.notifyAll();
            }

        });

        Thread c = new Thread(() -> {
            synchronized (lock) {
                if (!threadB) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("c");

        });

        a.start();
        b.start();
        c.start();


    }
}
